<?php

/**
 * This is the model class for table "tbl_form".
 *
 * The followings are the available columns in table 'tbl_form':
 * @property string $id
 * @property string $name
 * @property integer $type
 * @property string $user_create
 * @property string $created_at
 * @property string $user_update
 * @property string $updated_at
 * @property double $price
 * @property integer $status
 * @property integer $is_checked
 * @property string $checked_at
 * @property string $user_check
 * @property string $cause
 * @property double $paid
 * @property double $remain
 * @property string $image_url
 *
 * The followings are the available model relations:
 * @property User $userCreate
 * @property User $userUpdate
 * @property User $userCheck
 * @property FormMeta[] $formMetas
 */
class Form extends CActiveRecord {

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'tbl_form';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('type, status, is_checked', 'numerical', 'integerOnly' => true),
            array('price', 'numerical'),
            array('name', 'length', 'max' => 255),
            array('image_url', 'length', 'max' => 255, 'tooLong' => '{attribute} is too long (max {max} chars).'),
//            array('image_url', 'file', 'types' => 'jpg, jpeg, gif, png', 'maxSize' => 1024 * 1024 * 2, 'tooLarge' => 'Ảnh không được vượt quá 2MB.'),
            array('user_create, user_update, user_check', 'length', 'max' => 20),
            array('cause', 'length', 'max' => 500),
            array('created_at, updated_at, checked_at, image_url', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, name, type, user_create, created_at, user_update, updated_at, price, status, is_checked, checked_at, user_check, cause', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'userCreate' => array(self::BELONGS_TO, 'User', 'user_create'),
            'userUpdate' => array(self::BELONGS_TO, 'User', 'user_update'),
            'userCheck' => array(self::BELONGS_TO, 'User', 'user_check'),
            'formMetas' => array(self::HAS_MANY, 'FormMeta', 'form_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'name' => 'Tên',
            'type' => 'Loại',
            'user_create' => 'Người tạo',
            'created_at' => 'Ngày tạo',
            'user_update' => 'Người chỉnh sửa',
            'updated_at' => 'Ngày chỉnh sửa',
            'price' => 'Giá tin đăng',
            'status' => 'Trạng thái',
            'is_checked' => 'Kiểm duyệt',
            'checked_at' => 'Duyệt lúc',
            'user_check' => 'Người duyệt',
            'cause' => 'Lý do',
            'image_url' => 'Ảnh',
            'paid' => 'Đã thanh toán',
            'remain' => 'Còn lại',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('type', $this->type);
        $criteria->compare('user_create', $this->user_create, true);
        $criteria->compare('created_at', $this->created_at, true);
        $criteria->compare('user_update', $this->user_update, true);
        $criteria->compare('updated_at', $this->updated_at, true);
        $criteria->compare('price', $this->price);
        $criteria->compare('status', $this->status);
        $criteria->compare('is_checked', $this->is_checked);
        $criteria->compare('checked_at', $this->checked_at, true);
        $criteria->compare('user_check', $this->user_check, true);
        $criteria->compare('cause', $this->cause, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Form the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

}
